package com.jt.provider.controller;


import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Slf4j
@Component
public class SentinelBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       BlockException e) throws Exception {
        //设置响应数据编码
        response.setCharacterEncoding("utf-8");
        //告诉浏览器向它响应的内容类型,以及编码方式
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(429);
        PrintWriter out = response.getWriter();
        out.print("<h1>服务正忙,稍后再试</h1>");
        out.flush();
        out.close();

    }
}
